import request from "..";

const getCsrfToken = () => {
  const cookieValue = document.cookie
    .split('; ')
    .find(row => row.startsWith('csrftoken='))
    ?.split('=')[1];
  return cookieValue || '';
};
export const fetchCsrfToken = async () => {
  const token = localStorage.getItem('csrftoken')
  if(!token){
    const res = await request.get('/get-csrf-token');
    if(res.code === 200 && res.data.csrftoken){
      localStorage.setItem('csrftoken',res.data.csrftoken)
    }
  }
}
export const uploader = async (formData:any)=>{
  return request.post('/uploader',formData,{
      headers:{
          'Content-Type':'multipart/form-data',
          'X-CSRFToken':getCsrfToken()
      },
      withCredentials:true
  })
}